package com.heart.cn.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

/**
 * json工具类
 * 
 * @author wangzh
 * 2017年12月19日 上午10:23:29
 */
public final class JsonUtil {
	private final static Logger log = LoggerFactory.getLogger(JsonUtil.class);
	
	private static ObjectMapper objectMapper = new ObjectMapper();
	
	static{
		objectMapper.setSerializationInclusion(Include.NON_NULL); 
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setFilters(new SimpleFilterProvider().setFailOnUnknownId(false));
	}
	
	/**
	 * 对象转json字符串
	 * @param value
	 * @return
	 */
	public static String toJson(Object value){
	    try {
	    	String jsonResult = objectMapper.writeValueAsString(value);
			return jsonResult;
		} catch (JsonProcessingException e) {
			log.error(e.toString(),e);
		}
		return null;
	}
	
	/**
	 * json字符串转对象
	 * @param json 字符串
	 * @param cls 实体类
	 * @return
	 */
	public static <T> T fromJson(String json, Class<T> cls){  
        if(StringUtil.isEmpty(json)){  
            return null;      
        }  
        try {  
            return objectMapper.readValue(json, cls);  
        } catch (Exception e) {  
        	log.error(json+" "+e.toString(),e);
        }  
        return null;  
    }
	
	/**
	 * json对象转实体类List<Map>集合
	 * @param json
	 * @return
	 */
	public static <T> List<T> fromJsonArray(String json){  
        if(StringUtil.isEmpty(json)){  
            return null;      
        }  
        try {  
        	return  objectMapper.readValue(json, new TypeReference<List<T>>(){});  
        } catch (Exception e) {  
        	log.error(json+" "+e.toString(),e);
        }  
        return null;  
    } 
	
	
	/**
	 * 获取hbase接口返回状态
	 * @param json
	 * @return
	 */
	public static String getResultCode(String json){
		try {
			
			JsonNode root = objectMapper.readTree(json);

			JsonNode resultCode = root.path("resultCode");
			return resultCode.asText();
			
		} catch (Exception e) {
			log.error(json+" "+e.toString(),e);
		}
		return "";
	}
	
	/**
	 * 格式化hbase接口响应返回key和value
	 * @param json
	 * @return
	 */
	public static Map<String,String> getIndexData(String json){
		Map<String,String> hbaseValue=new HashMap<String,String>();
		try {
			JsonNode root = objectMapper.readTree(json);
			JsonNode content = root.path("content");

			JsonNode value, key;
			for (int i = 0; i < content.size(); i++) {
				value = content.get(i).path("familes").path("content").get(0).path("VALUE");
				key = content.get(i).path("key");
				hbaseValue.put(key.asText(), value.asText());
			}
		} catch (Exception e) {
			log.error(json+" "+e.toString(), e);
		}
		return hbaseValue;
	}
	
	/**
	 * 格式化hbase接口响应，返回value
	 * @param json
	 * @return
	 */
	public static List<String> getData(String json){
		List<String> hbaseData=new ArrayList<String>();
		try {
			JsonNode root = objectMapper.readTree(json);
			JsonNode content = root.path("content");

			JsonNode value;
			for (int i = 0; i < content.size(); i++) {
				value = content.get(i).path("familes").path("content").get(0).path("VALUE");
				hbaseData.add(value.asText());
			}
		} catch (Exception e) {
			log.error(json+" "+e.toString(), e);
		}
		return hbaseData;
	}
}
